/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.ac.cput.pretorius.programs.program06.impl;

import za.ac.cput.pretorius.programs.program06.IdTester;

/**
 *
 * @author griddy
 */
public class IdTesterImpl implements IdTester {

    
    
    private String idNum;
    
    @Override
    public void setId(String id) throws Exception {
        if( id.length() != 13 )
            throw new Exception();
        
        idNum = id;
            
    }

    @Override
    public String getGender() {
       if(Integer.parseInt( idNum.charAt(6)+"") >4)
           return "Male";
       else
           return "Female";
    }

    @Override
    public boolean isCitizen() {
       
        return (idNum.charAt(11) == '8');
    }

    @Override
    public boolean validateId() {
        
        int odd = 0;
        int even = 0;
        int temp = 0;
        String evenStr = "";
        
        boolean idFlag = false;
        
        for(int i = 0 ; i <13 ; i++)
        {
           if(i%2 != 0)
           {
              evenStr += idNum.charAt(i) + ""; 
               
           }
           else if (i < 12) 
              odd += Integer.parseInt(idNum.charAt(i) + "" );
               
        }
        
        even = Integer.parseInt(evenStr);
        even *= 2;
        evenStr = even+"";
        even = 0;
        
        for(int i = 0 ; i <evenStr.length() ; i++)
        {
            even += Integer.parseInt(evenStr.charAt(i) + "" ); 
        }
        even += odd;
        evenStr = even+"";

        temp = 10 - Integer.parseInt(evenStr.charAt(1)+"" );
        
       
        if(Integer.parseInt( idNum.charAt(12)+"" ) == temp)
        
            idFlag = true;
     
        else if ( temp>9) //2 digits or more
            if (idNum.charAt(12) == (temp +"").charAt(1) )
                idFlag = true;

        return idFlag;
    }
    
}
